package com.glela.api.im.rongcloud;

import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.List;

import com.glela.common.beans.FormatType;
import com.glela.api.im.rongcloud.vo.SdkHttpResult;
import com.glela.api.im.util.IMConfig;

import cn.jpush.api.push.model.Message;

public class ApiHttpClient {

	private static final String UTF8 = "UTF-8";

	// 获取token
	public static SdkHttpResult getToken(String userId, String userName,
			String portraitUri, FormatType format) throws Exception {
		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/user/getToken." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, UTF8));
		sb.append("&name=").append(URLEncoder.encode(userName == null ? "" : userName, UTF8));
		sb.append("&portraitUri=").append(URLEncoder.encode(portraitUri == null ? "" : portraitUri, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 发送消息(push内容为消息内容)
	public static SdkHttpResult publishMessage(String fromUserId,
			List<String> toUserIds, Message msg, FormatType format) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/message/private/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=").append(URLEncoder.encode(msg.toString(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 发送消息(可传递push内容)
	public static SdkHttpResult publishMessage(String fromUserId,
			List<String> toUserIds, Message msg, String pushContent, String pushData, FormatType format)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/message/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=").append(URLEncoder.encode(msg.toString(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(URLEncoder.encode(pushContent, UTF8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData, UTF8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 发送系统消息
	public static SdkHttpResult publishSystemMessage(String fromUserId,
			List<String> toUserIds, Message msg, String pushContent, String pushData, FormatType format)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/message/system/publish." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("fromUserId=").append(URLEncoder.encode(fromUserId, UTF8));
		if (toUserIds != null) {
			for (int i = 0; i < toUserIds.size(); i++) {
				sb.append("&toUserId=").append(URLEncoder.encode(toUserIds.get(i), UTF8));
			}
		}
		sb.append("&objectName=").append(URLEncoder.encode(msg.toString(), UTF8));
		sb.append("&content=").append(URLEncoder.encode(msg.toString(), UTF8));

		if (pushContent != null) {
			sb.append("&pushContent=").append(URLEncoder.encode(pushContent, UTF8));
		}

		if (pushData != null) {
			sb.append("&pushData=").append(URLEncoder.encode(pushData, UTF8));
		}

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 获取消息历史记录下载地址
	public static SdkHttpResult getMessageHistoryUrl(String date, FormatType format)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/message/history." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}

	// 删除消息历史记录
	public static SdkHttpResult deleteMessageHistory(String date, FormatType format)
			throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(IMConfig.appKey, IMConfig.appSecret,
				IMConfig.uri + "/message/history/delete." + format.toString());

		StringBuilder sb = new StringBuilder();
		sb.append("date=").append(URLEncoder.encode(date, UTF8));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
}
